/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tradeplus.jms;

import com.tradeplus.util.ConnectionManager;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import java.io.*;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.naming.*;

/**
 *
 * @author ronnie
 */
public class JMSMessage {
    
    public final static String JMS_FACTORY = "javax.jms.TopicConnectionFactory";
    protected TopicConnectionFactory tconFactory;
    protected TopicConnection tcon;
    protected TopicSession tsession;
    protected TopicPublisher tpublisher;
    protected Topic topic;
    protected MapMessage msg;
    protected ObjectMessage omsg;
    
    public JMSMessage() {
        try {
            Context cotx = ConnectionManager.getInitialContext();
            init(cotx,"epmTopic");
            cotx.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    
    /**
     * Create all the necessary objects for sending
     * messages to a JMS Topic.
     */
    public void init(Context ctx, String topicName) throws NamingException, JMSException {
        tconFactory = (TopicConnectionFactory)ctx.lookup(JMS_FACTORY);
        tcon = tconFactory.createTopicConnection();
        tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        
        try {
            topic = (Topic)ctx.lookup(topicName);
        } catch(NamingException ne) {
            System.out.println("Naming Exception");
            topic = tsession.createTopic(topicName);
            ctx.bind(topicName, topic);
        }
        
        tpublisher = tsession.createPublisher(topic);
        msg = tsession.createMapMessage();
        msg.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
        omsg = tsession.createObjectMessage();
        omsg.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
        tcon.start();
    }
    
    public void addMsg(String name, String value) {
        try {
            msg.setString(name, value);
            System.out.println("message content [" + name + "]=[" + value + "]");
        } catch(JMSException jmse) {
            System.out.println("addMsg content [" + name + "]=[" + value + "] caught JMSException");
            jmse.printStackTrace();
        }
    }
    
    public void addMsg(String name, int value) {
        try {
            msg.setInt(name, value);
            System.out.println("message content [" + name + "]=[" + value + "]");
        } catch(JMSException jmse) {
            System.out.println("addMsg content [" + name + "]=[" + value + "] caught JMSException");
            jmse.printStackTrace();
        }
    }

    public void addMsg(String name, boolean value) {
        try {
            msg.setBoolean(name, value);
            System.out.println("message content [" + name + "]=[" + value + "] caught JMSException");
        } catch(JMSException jmse ){
            System.out.println("addMsg content [" + name + "]=[" + value + "] caught JMSException");
            jmse.printStackTrace();
        }
    }

    public void addMsg(Serializable value) {
        try {
            omsg.setObject(value);
            System.out.println("message content [" + value + "]");
        } catch(JMSException jmse ) {
            System.out.println("addMsg content [" + value + "] caught JMSException");
            jmse.printStackTrace();
        }
    }
        
    public boolean send() {
        try {
            tpublisher.publish(msg);
            tpublisher.close();
            tsession.close();
            tcon.close();
            return true;
        } catch(JMSException jmse) {
            System.out.println("jms message failed to send");
            jmse.printStackTrace();
            return false;
        }
    }
    
    public boolean sendWithObject() {
        try {
            tpublisher.publish(omsg);
            tpublisher.close();
            tsession.close();
            tcon.close();
            return true;
        } catch(JMSException jmse) {
            System.out.println("jms message failed to send");
            jmse.printStackTrace();
            return false;
        }
    }
    
}
